// pages/doctor-course-pay/doctor-course-pay.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgBase: app.globalData.imgUrl,          // 线上图片
    localImg: app.globalData.staticimgUrl,   // 本地图片
    pirce:'',          // 课程价格
    maxCoin: '',       // 美丽币
    maxPrice: '',      // 美丽钱数
    cur: true,         // 否抵扣
    beautyCoin:'',     // 美丽币后台用
    totalPrice:'',
    payType: '',       // 支付类型1=美丽币；2=支付宝；3=微信 4 微信+美丽币
    userUseCoin:'',
    coinShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pirce: options.pirce,
      id: options.courseid
    })
    this.beautifulCoin();
  },

  //获取美丽币
  beautifulCoin() {
    const that = this;
    const totalPrice = this.data.pirce;
    app.request({
      url: "doctor/beautyCoin/getBeautyCoin",
      success: function (res) {
        const maxPrice = res.data.maxPrice;
        const maxCoin = res.data.maxCoin;
        const valueMoney = res.data.valueMoney;
        const result = maxCoin / maxPrice;
        // 商品价格大于抵扣数
        if (totalPrice > maxPrice) {
          that.setData({
            maxCoin: res.data.maxCoin,
            maxPrice: res.data.maxPrice
          })
        } else if (totalPrice <= maxPrice && totalPrice >= valueMoney) {

          that.setData({
            maxCoin: result * parseInt(totalPrice),
            maxPrice: parseInt(totalPrice)
          })
        } 
        else if (totalPrice < valueMoney) {

          that.setData({
            coinShow: true
          })
        }
        // that.setData({
        //   maxCoin: res.data.maxCoin,
        //   maxPrice: res.data.maxPrice,
        //   valueMoney: res.data.valueMoney,
        //   userUseCoin: res.data.userUseCoin,
        // })
      },
      fail: function (res) {}
    });
  },
  // 是否抵扣
  selection(){
    if (this.data.maxPrice<1){
      app.showModal("无法使用美丽币抵换")
      return
    }
    this.setData({
      cur:!this.data.cur
    })
    console.log(this.data.cur)
  },
  // 判断是否抵现
  whetherPay(){
    const that = this;
    // 是否可用抵扣
    if (this.data.pirce < this.data.valueMoney && !this.data.cur){

      app.showModal("商品价格低于最低抵扣不可用")
      return
    }else{
      // 不使用美丽币
      if (this.data.cur) { 
        let totalPrice = this.data.pirce
        this.setData({
          totalPrice
        }) 
        this.setData({
          payType: 3,
          beautyCoin: 0
        })
        that.pay();
      // 完全使用美丽币/ this.data.valueMoney *10
      } else if (!this.data.cur && this.data.pirce - this.data.maxPrice <= 0) { 
        this.setData({
          payType: 1,
          beautyCoin: this.data.pirce * 10 
        })
        that.payIcon();
      // 微信 + 美丽币
      } else if (!this.data.cur && this.data.pirce - this.data.maxPrice >= 0){
        let totalPrice = this.data.pirce - this.data.maxPrice 
        this.setData({
          totalPrice
        })
        this.setData({     
          payType: 4,
          beautyCoin: this.data.maxCoin
        })
        that.pay();
      }
    }
  },
  // 完全美丽币支付
  payIcon(){
    const consumerId = wx.getStorageSync('userid');
    const that = this;
    const param = {
      consumerId: consumerId,
      courseId: this.data.id,
      payType: this.data.payType,
      beautyCoin: this.data.beautyCoin
    };
    app.request({
      url: "train/course/generateCourseOrder",
      data: param,
      success: function (res) {
        wx.navigateTo({
          url: '../doctor-course-payEnd/doctor-course-payEnd?orderid=' + res.orderId + "&beautyCoin=" + that.data.beautyCoin + "&totalPrice=" + that.data.totalPrice
        });
      },
      fail: function (res) {

      }
    });
  },
  // 支付
  pay() {
    const consumerId = wx.getStorageSync('userid');
    const that = this;
   
    const param = {
      consumerId: consumerId,
      courseId: this.data.id,
      payType: this.data.payType,
      beautyCoin: this.data.beautyCoin
    };
    app.request({
      url: "train/course/generateCourseOrder",
      data: param,
      success: function(res) {
        wx.navigateTo({
          url: '../doctor-course-weChatpay/doctor-course-weChatpay?orderid=' + res.orderId + "&beautyCoin=" + that.data.beautyCoin + "&totalPrice=" + that.data.totalPrice
        });
        // res.code == 200 || that.postOrderid(res.orderId)
      },
      fail: function(res) {
        
      }
    });
  },

 
})